package com.learning.JavaSE.workExercise.module4.Task2;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;


/**
 * 题目：编写程序，循环接收用户从键盘输入多个字符串，直到输入“end”时循环结束，并将所有已输入的字符串按字典顺序倒序录入到项目下abc.txt中
 */
public class FileABC {
    public static void main(String[] args) {
        PrintStream txt = null;
        BufferedReader br = null;
        try {
            System.out.println("开始输入字符串（输入'end'结束）：");
            br = new BufferedReader(new InputStreamReader(System.in));
            txt = new PrintStream(new FileOutputStream("d:/PersonalCodeLearning/src/com/learning/lagou/JavaSE/workExercise/module4/Task2/abc.txt"));
            ArrayList<String> inputList = new ArrayList<>();
            String input = br.readLine();
            while (!"end".equals(input)) {
                inputList.add(input);
                input = br.readLine();
            }
            Collections.sort(inputList); //按字典顺序排序
            Collections.reverse(inputList); //倒序排列
            for (String s : inputList) {
                txt.println(s);
            }
            System.out.println("成功将输入的字符串倒序录入到文件abc.txt中。");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 3.关闭流对象并释放有关的资源
            // 4.关闭流对象并释放有关的资源
            if (null != txt) {
                txt.close();
            }
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
